/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spotkanie;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mateusz.ledzewicz
 */
public class Policeman extends Thread {
    private final CrossObject cross;
    
    public Policeman(CrossObject cross){
        this.cross = cross;
    }
    
    @Override
    public void run(){ 
          while(true){
            try {
                synchronized(cross){
                    synchronized (cross.cars) {
                        int numbersOfCars = 0;
                        for (CarThread car : cross.cars){
                            if(car.isOnCross()){
                                numbersOfCars++;
                            }
                        }
                        if(numbersOfCars > 1)
                            throw new Exception("Colision "+ numbersOfCars);
                    }
                }
                
                
            } catch (Exception ex) {
                    Logger.getLogger(Policeman.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
              
    }
    
}
